﻿<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#-- Freemark的包含指令 -->
    <#include "../common/header.ftl"/>
    <#--treeview-->
    <link rel="stylesheet" href="/js/plugins/treeview/bootstrap-treeview.min.css" type="text/css"/>
    <script type="text/javascript" src="/js/plugins/treeview/bootstrap-treeview.min.js"></script>
<script>
    $(function () {
        var insertJson;//更新操作的回显
        //获取根地区数据
        $.get("/region/selectTreeViewByParentId.do",{type:"tree"},function (data) {
            var defaultData = [
                {
                    text: '全部地区',
                    nodes: data
                }
            ]
            //将数据渲染进treeview容器中
            $('#treeview').treeview({
                data: defaultData,
                showTags: true,//开启标签显示
                lazyLoad:function(node){//参数1:点击的当前节点
                    //console.log(node);
                    //获取子地区数据
                    $.get("/region/selectTreeViewByParentId.do",{parentId:node.id,type:"tree"},function (data) {
                        //console.log(data);
                        //把子地区放到node下(参数1:要添加的节点数据;参数2:父节点是谁)
                        $('#treeview').treeview('addNode', [ data, node ]);
                    });
                },

                onNodeSelected:function(even,node){//节点被选中事件
                    $.get("/region/selectTreeViewByParentId.do",{parentId:node.id},function(data){
                        //将选中节点的值存入更新回显的容器中
                        insertJson = {id:node.id,name:node.text}
                        console.log(insertJson);
                        console.log(data);
                        //清空tbody数据
                        $(".regionTable tbody").empty();
                        $.each(data,function (index,ele) {
                            //拷贝模板
                            var temp = $("#tempTable tr").clone(true);//深度克隆(把事件也拷贝过去)
                            //填充数据
                            $(temp).find("td:nth-child(1)").html(index+1);
                            $(temp).find("td:nth-child(2)").html(ele.name);
                            //设置到tbody上
                            $(temp).find("a").attr("data-json",ele.json);
                            //根据state修改页面按钮显示
                            console.log('状态'+ele.state);
                            if (ele.state==1) {
                                $(temp).find("a:last").html("<span class=\"glyphicon glyphicon-trash\"></span>取消推荐");
                            }
                            $(".regionTable tbody").append(temp);
                        });
                    });
                }
            });
        });
        //给添加和编辑按钮添加点击事件
        $(".inputBtn").click(function () {
            //清空模态框上次的回显
            $("#editForm input").val("");
            //回显数据(当前点击事件绑定的json)
            var json = $(this).data("json");
            //编辑操作的回显
            if (json) {
                $("#editForm input[name='id']").val(json.id);
                $("#editForm input[name='name']").val(json.name);
                $("#editForm input[name='parent.id']").val(json.parentId);
                if (json.parentName) {
                    $("#editForm input[id='parentName']").val(json.parentName);
                }else{
                    $("#editForm input[id='parentName']").val("全部地区");
                }
            }else{//给新增操作设置回显
                console.log(insertJson);
                if (insertJson) {
                    $("#editForm input[name='parent.id']").val(insertJson.id);
                    $("#editForm input[id='parentName']").val(insertJson.name);
                }else{
                    $("#editForm input[id='parentName']").val("全部地区");
                }
            }
            //显示模态框
            $("#inputModal").modal("show");
            //给模态框提交按钮绑定点击事件
            $(".modalInputBtn").click(function () {
                //提交模态框表单
                $("#editForm").ajaxSubmit(function (data) {
                    $.messager.alert("温馨提示","编辑成功,1.5秒后后跳转");
                    setTimeout(function () {
                        window.location.reload();//刷新当前页面
                    },1500)
                });
            });
        });
        //给推荐按钮绑定点击事件
        $(".changeStatusBtn").click(function () {
                var json = $(this).data("json");
                var state = json.state;
                //获取当前标签的id和status状态
                if(state==1){
                    state = 0;
                }else {
                    state = 1;
                }
                $.get("/region/changeState.do",{id:json.id,state:state},function (data) {
                    $.messager.alert("温馨提示","设置成功,1.5秒后后跳转");
                    setTimeout(function () {
                        window.location.reload();//刷新当前页面
                    },1500)
                })
        });
    });
</script>
</head>
<body>
<#--准备列表模板-->
<table id="tempTable" style="display: none">
    <tr>
        <td></td>
        <td></td>
        <td>
            <a class="btn btn-info btn-xs btn-input inputBtn" >
                <span class="glyphicon glyphicon-pencil"></span>编辑
            </a>

            <a class="btn btn-danger btn-xs btn-hot changeStatusBtn" >
                <span class="glyphicon glyphicon-trash"></span>设为推荐
            </a>
        </td>
    </tr>
</table>
<div class="container " style="margin-top: 20px">
    <#--欢迎-->
    <#include "../common/top.ftl"/>

    <div class="row">
        <div class="col-sm-3">
            <#-- Freemark设置共享变量的指令 -->
            <#assign menu="region"/>
            <#--左侧菜单-->
            <#include "../common/menu.ftl"/>
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">地区管理</h1>
                </div>
            </div>
            <!--高级查询--->
            <form class="form-inline" id="searchForm" action="/region/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <a href="javascript:;" class="btn btn-success btn-input inputBtn">添加</a>
            </form>
        <#--栅格系统-->
            <div class="row">
                <div class="col-sm-4">
                    <div id="treeview"></div>
                </div>

                <div class="col-sm-8">
                    <table class="table table-striped table-hover regionTable" >
                        <thead>
                        <tr>
                            <th>编号</th>
                            <th>名称</th>
                        </tr>
                        </thead><tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/region/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" name="id">
                    <div class="form-group">
                        <label for="name" class="col-sm-4 control-label">名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="name" name="name"
                                   placeholder="请输入地区名称">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sn" class="col-sm-4 control-label">上级地区：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="parentName" readonly>
                            <input type="hidden" class="form-control" id="parentId" name="parent.id" >
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary btn-submit modalInputBtn">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</body>
</html>